package 线程池.父子线程问题;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

/**
 * @author caihe
 * @date 2022/7/19 13:35
 */
public class Test {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        CountDownLatch a = new CountDownLatch(1);
        // 1
        Future<String> aaa = ThreadPool.executorService.submit(() -> father("aaa"));
        // 2
        Future<String> bbb = ThreadPool.executorService.submit(() -> father("bbb"));
//        Future<String> asd = ThreadPool.executorService.submit(() -> fun("bbb"));
//        Future<String> d = ThreadPool.executorService.submit(() -> fun("bbb"));
//        Future<String> f = ThreadPool.executorService.submit(() -> fun("bbb"));
//        Future<String> c = ThreadPool.executorService.submit(() -> fun("bbb"));
//
        System.out.println(aaa.get());
        System.out.println(bbb.get());
//        asd.get();
//        d.get();
//        f.get();
//        c.get();
        System.out.println();
        a.await();
        ThreadPool.executorService.shutdown();
    }
    
    
    
    public static String father(String str) {
        System.out.println(str + " father begin - " + Thread.currentThread().getName());
        try {
//            ThreadPool.executorService.execute(() -> son(str ));
            Thread.sleep(1000);
            // 3
            Future<String> submit = ThreadPool.executorService.submit(() -> son(str));
            
            
            
            submit.get();
        } catch (Exception e) {
            e.printStackTrace();
        }
        System.out.println(str + " father end - " + Thread.currentThread().getName());
        return str;
    }

    public static String son(String str) {
        System.out.println(str + " son begin - " + Thread.currentThread().getName());
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(str + " son end - " + Thread.currentThread().getName());
        return str;
    }
    
    
}
